﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Configuration;
using KPIS.DBM;
using KPIS.GERP.GHRMS.MODEL;
using KPIS.GERP.GHRMS.IDAL;

namespace KPIS.GERP.GHRMS.DAL
{
    public sealed class MasCountryZoneDAL : IMasCountryZoneDAL
    {
        IDBManager dbManager = new DBManager(SystemInfo._DataProvider, SystemInfo._ConnectionString);
        //IDBManager dbManager = new DBManager(DataProvider.MySQL, "server=192.168.17.2; user id=root;pwd=root123456789;database=gerp");

        public MasCountryZoneDAL()
        {
        
        }
        public MasCountryZoneInfo GetMasCountryZoneInfo(Nullable<int> seq)
        {
            string strSQL;
            MasCountryZoneInfo Info = new MasCountryZoneInfo();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@seq", seq);
                dbManager.AddParameters(1, "@DeleteRecord", SystemInfo._DeleteRecordStatus);

                strSQL = "SELECT ZONE_SEQ, ZONE_NAME, ZONE_NAME_ENG, REMARKS"
                    + " FROM MAS_COUNTRY_ZONES"
                    + " WHERE RECORD_STATUS not like @DeleteRecord"
                         + " AND ZONE_SEQ = @seq";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    Info.Sequence = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                    Info.Name = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
                    Info.NameEng = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
                    Info.Remarks = rdr.IsDBNull(3) ? "" : rdr.GetString(3);
                }

                return Info;
            }
            catch
            {
                return null;
                throw;
            }
            finally
            {
                dbManager.CloseReader();
                dbManager.Dispose();
            }
        }
        public IList<MasCountryZoneInfo> ListMasCountryZones(MasCountryZoneInfo zoneInfo)
        {
            string strSQL;

            List<MasCountryZoneInfo> infoList = new List<MasCountryZoneInfo>();

            try
            {
                dbManager.Open();
                dbManager.CreateParameters(2);

                dbManager.AddParameters(0, "@DeleteRecord", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(1, "@Sequence", zoneInfo.Sequence);

                strSQL = "SELECT ZONE_SEQ, ZONE_NAME, ZONE_NAME_ENG, REMARKS"
                     + " FROM MAS_COUNTRY_ZONES"
                     + " WHERE RECORD_STATUS not like @DeleteRecord"
                     + (zoneInfo.Sequence > 0 ? " AND ZONE_SEQ = @Sequence" : "")
                     + " ORDER BY ZONE_NAME";

                IDataReader rdr = dbManager.ExecuteReader(CommandType.Text, strSQL);

                while (rdr.Read())
                {
                    MasCountryZoneInfo Info = new MasCountryZoneInfo();

                    Info.Sequence = rdr.IsDBNull(0) ? (int?)null : rdr.GetInt32(0);
                    Info.Name = rdr.IsDBNull(1) ? "" : rdr.GetString(1);
                    Info.NameEng = rdr.IsDBNull(2) ? "" : rdr.GetString(2);
                    Info.Remarks = rdr.IsDBNull(3) ? "" : rdr.GetString(3);

                    infoList.Add(Info);
                }
            }
            catch (Exception ex)
            {
                throw (ex);
            }
            finally
            {
                dbManager.Dispose();
            }

            return infoList;
        }
        public void Insert(MasCountryZoneInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Name", Info.Name);
                dbManager.AddParameters(1, "@NameEng", Info.NameEng);
                dbManager.AddParameters(2, "@Remarks", Info.Remarks);
                dbManager.AddParameters(3, "@RecordStatus", SystemInfo._ActiveRecordStatus);
                dbManager.AddParameters(4, "@CreatedBy", Info.CreatedBy);
                dbManager.AddParameters(5, "@CreatedWhen", DateTime.Today);

                strSQL = "INSERT INTO MAS_COUNTRY_ZONES(ZONE_NAME, ZONE_NAME_ENG, REMARKS"
                        + ", RECORD_STATUS, CREATED_BY, CREATED_WHEN)"
                    + " VALUES(@Name, @NameEng, @Remarks, @RecordStatus, @CreatedBy, @CreatedWhen)";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Update(MasCountryZoneInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(6);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@Name", Info.Name);
                dbManager.AddParameters(2, "@NameEng", Info.NameEng);
                dbManager.AddParameters(3, "@Remarks", Info.Remarks);
                dbManager.AddParameters(4, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(5, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_COUNTRY_ZONES"
                           + " SET ZONE_NAME = @Name"
                                + ", ZONE_NAME_ENG = @NameEng"
                                + ", REMARKS = @Remarks"
                                + ", UPDATED_BY = @UpdatedBy"
                                + ", UPDATED_WHEN = @UpdatedWhen"
                           + " WHERE ZONE_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
        public void Delete(MasCountryZoneInfo Info)
        {
            string strSQL;

            try
            {
                dbManager.Open();
                dbManager.BeginTransaction();
                dbManager.CreateParameters(4);

                dbManager.AddParameters(0, "@Seq", Info.Sequence);
                dbManager.AddParameters(1, "@RecordStatus", SystemInfo._DeleteRecordStatus);
                dbManager.AddParameters(2, "@UpdatedBy", Info.UpdatedBy);
                dbManager.AddParameters(3, "@UpdatedWhen", DateTime.Today);

                strSQL = "UPDATE MAS_COUNTRY_ZONES"
                    + " SET RECORD_STATUS = @RecordStatus"
                        + ", UPDATED_BY = @UpdatedBy"
                        + ", UPDATED_WHEN = @UpdatedWhen"
                    + " WHERE ZONE_SEQ = @Seq";

                dbManager.ExecuteNonQuery(CommandType.Text, strSQL);
                dbManager.CommitTransaction();
            }
            catch
            {
                dbManager.RollBackTransaction();
                throw;
            }
            finally
            {
                dbManager.Dispose();
            }
        }
    }
}
